package exttable.testversioning.testversionrunner;

import exttable.testversioning.IDBUtils;
import exttable.testversioning.TestVersioningDBUtils;
import exttable.view.ViewNodes;
import exttable.view.ViewPhases;
import utils.Localisation;

import javax.swing.*;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * Created by IntelliJ IDEA.
 * User: Admin
 * Date: Aug 11, 2010
 * Time: 5:05:27 PM
 * To change this template use File | Settings | File Templates.
 */
public class TestVersionController {

    TestVersionModel model;
    TestVersionView view;
    private IDBUtils dataService;

    public TestVersionController(TestVersionModel model, TestVersionView view) {
        this.model = model;
        this.view = view;
        dataService = new TestVersioningDBUtils();
//        view.addFillAnalysisAction(new FillAction());
        view.addSaveAnalysisAction(new SaveAction());
        view.addTreeSelection(new TreeListener());
    }

    class FillAction implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            if (view.getUnitTable().getCellEditor() != null) {
                view.getUnitTable().getCellEditor().stopCellEditing();
            }

            ViewPhases phase = (ViewPhases) view.getPhaseList().getSelectedItem();
            if (phase.getPhaseId() != null) {
                if (dataService.processAnalysisInPhases(view.getUnitTable(), phase)) {
//                    view.getSaveAnalysisInPhases().setVisible(true);
//                    view.getFillPhasesWithAnalysis().setVisible(false);
                    view.getPhaseList().setEnabled(false);
                }
            } else {
                JOptionPane.showMessageDialog(null, Localisation.getMessage("error.phasenotselected")
                        , Localisation.getMessage("error.title")
                        , JOptionPane.WARNING_MESSAGE);
            }
        }
    }

    class SaveAction implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            if (dataService.saveAnalysisInPhases(view.getUnitTable(), (ViewPhases) view.getPhaseList().getSelectedItem())) {
                view.viewAnalysisInPhases();
            }
//            view.getSaveAnalysisInPhases().setVisible(false);
        }
    }

    class TreeListener implements TreeSelectionListener {
        @Override
        public void valueChanged(TreeSelectionEvent e) {
            DefaultMutableTreeNode node = (DefaultMutableTreeNode) view.getControlTree().getLastSelectedPathComponent();
            ViewNodes vn = (ViewNodes) node.getUserObject();
            menuController(vn);
        }

        private void menuController(ViewNodes vn) {
            try {
                switch (vn.getNodeType()) {
                    case VIEW_PHASE:
                        view.viewAnalysisInPhases();
//                        view.getSaveAnalysisInPhases().setVisible(false);
                        hideShowComponents(false);
                        break;
                    case CREATE_PHASE:
                        view.displayAnalysisPhasesFillList();
//                        view.getInfoSelectAnalysis().setVisible(true);
//                        view.getPhaseLabel().setVisible(true);
                        view.getPhaseList().setVisible(true);
                        view.getPhaseList().setEnabled(true);
//                        view.getFillPhasesWithAnalysis().setVisible(true);
                        hideShowComponents(true);
                        break;
                    case CREATE_ANALYSIS_VERSION:
                        view.displayAnalysisVersionCreateList();
                        hideShowComponents(false);
                    default:
                }
            }
            catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    private void hideShowComponents(boolean show) {
//        view.getInfoSelectAnalysis().setVisible(show);
//        view.getPhaseLabel().setVisible(show);
        view.getPhaseList().setVisible(show);
//        view.getFillPhasesWithAnalysis().setVisible(show);
    }
}
